package FlyGame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class EnterPanel extends JPanel {

    FlyFrame flyFrame;
    public int current = 0;
    BGM bgm = new BGM();
//    RankFram rankFram=new RankFram();

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        this.setLayout(null);
        //绘制背景图片
        FlyPicture.BackPicture.paintIcon(this, g, 0, 0);

        //开始按钮
        JButton start = new JButton(FlyPicture.Start);
        start.setBounds(100,300,150,80);
        start.setOpaque(false);
        start.setBorder(null);
        start.setContentAreaFilled(false);

        //开始按钮添加监听事件
        start.addActionListener(new StartAction());

        //排行榜按钮
        JButton rank = new JButton(FlyPicture.Rank);
        rank.setBounds(350,300,150,80);
        rank.setOpaque(false);
        rank.setBorder(null);
        rank.setContentAreaFilled(false);
        rank.addActionListener(new RankShow());

        this.add(start);
        this.add(rank);

        //绘制标题 Fly Bird
        g.setColor(Color.white);
        g.setFont(new Font("微软雅黑", Font.BOLD, 50));
        g.drawString("Fly Brid", 200, 120);

        paintSkin(g);

        //切换皮肤按钮
        JButton changeBtn = new JButton(FlyPicture.Arrow);
        changeBtn.setBounds(320,182,25,25);
        changeBtn.setOpaque(false);
        changeBtn.setBorder(null);
        changeBtn.setContentAreaFilled(false);
        //添加监听事件
        changeBtn.addActionListener(new ChangeAction());
        this.add(changeBtn);

        //背景音乐开始按钮
        JButton broadcast = new JButton(FlyPicture.Broadcast);
        broadcast.setBounds(5,5,70,70);
        broadcast.setOpaque(false);
        broadcast.setBorder(null);
        broadcast.setContentAreaFilled(false);
        broadcast.addActionListener(new MusicStart());
        this.add(broadcast);

        //背景音乐暂停按钮
        JButton stop = new JButton(FlyPicture.Stop);
        stop.setBounds(70,5,70,70);
        stop.setOpaque(false);
        stop.setBorder(null);
        stop.setContentAreaFilled(false);
        stop.addActionListener(new MusicStop());
        this.add(stop);

        //登录按钮
        JButton login = new JButton(FlyPicture.Login);
        login.setBounds(500,5,70,70);
        login.setOpaque(false);
        login.setBorder(null);
        login.setContentAreaFilled(false);
        login.addActionListener(new LoginShow());
        this.add(login);

    }

    public void paintSkin(Graphics g){
        if(current == 0) {
            FlyPicture.Bird.paintIcon(this,g,265,180);
        }
        else if(current == 1){
            FlyPicture.Bird2.paintIcon(this,g,265,180);
        }
        else{
            FlyPicture.Bird3.paintIcon(this,g,265,180);
        }
        repaint();
    }

    class StartAction implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            flyFrame = new FlyFrame(current);
        }
    }

    class ChangeAction implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            if(current == 2){
                current = 0;
            }
            else{
                current ++ ;
            }
        }
    }

    class MusicStart implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            bgm.play();
        }
    }

    class MusicStop implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            bgm.stop();
        }
    }

    class RankShow implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
//            rankFram.setVisible(true);
            new RankFram();
        }
    }

    class LoginShow implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            LoginFrame loginFrame = new LoginFrame();
            loginFrame.setVisible(true);
        }
    }

}
